package com.ict.ais.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ict.ais.common.StateEnum;
import com.ict.ais.common.StateMsg;
import com.ict.ais.realTime.facade.api.IRealTimeMMSIService;
import com.ict.ais.realTime.facade.dto.CountryTPCount;
import com.ict.ais.realTime.facade.dto.ResultObj;
import com.ict.ais.realTime.facade.model.AppointTarget;
import com.ict.ais.realTime.facade.model.AppointTargetFuseRst;
import com.ict.ais.realTime.facade.model.RealTimeMMSI;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by chenlong on 2016/11/21.
 */
@Controller
@RequestMapping({"/realtime"})
public class RealTimeMMSIController {
    @Resource
    IRealTimeMMSIService realTimeMMSIService;

    /**
     * @param map
     * @param ldlon   左下经度
     * @param ldlat   左下纬度
     * @param rulon   右上经度
     * @param rulat   右上纬度
     * @param limit   返回目标个数限制,最大值：不做限制
     * @param timeout 消批时间,0：不做消批；单位分钟
     * @param mode    模式,0：融合模式；1：原始模式
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "areaships.do", method = RequestMethod.POST)
    public String getDegInfo(ModelMap map,
                             @RequestParam(value = "ldlon", required = false) double ldlon,
                             @RequestParam(value = "ldlat", required = false) double ldlat,
                             @RequestParam(value = "rulon", required = false) double rulon,
                             @RequestParam(value = "rulat", required = false) double rulat,
                             @RequestParam(value = "limit", required = false) int limit,
                             @RequestParam(value = "timeout", required = false) int timeout,
                             @RequestParam(value = "mode", required = false) int mode) {
        String msg;
        String jsonStr;
        if (timeout < 0) {
            msg = "消批时间不能小于0";
            map.addAttribute("error", msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }

        List<RealTimeMMSI> realTimeMMSIList = realTimeMMSIService.getDegInfo(ldlon, ldlat, rulon, rulat, limit, timeout, mode);
        if (realTimeMMSIList.size()==0) {
            msg = "获取实时船舶信息失败";
            map.addAttribute("error", msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }
        map.addAttribute("shipList", realTimeMMSIList);
        jsonStr = StateMsg.toJson(1, map);
        return jsonStr;
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * 实时重点目标船舶的获取
     * @param map
     * @param limit
     * @param timeout
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "imtargetships.do", method = RequestMethod.POST)
    public String getImportantTarget(ModelMap map,
                             @RequestParam(value = "limit", required = false) int limit,
                             @RequestParam(value = "timeout", required = false) int timeout) {
        String msg;
        String jsonStr;
        if (timeout < 0) {
            msg = "消批时间不能小于0";
            map.addAttribute("error", msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }

        ResultObj resultObj = realTimeMMSIService.getImportantTarget(limit,timeout);
        if(resultObj.getState()==0){
            msg="列表为空";
            map.addAttribute("error",msg);
            jsonStr = StateMsg.toJson(0, map);
            return jsonStr;
        }else if(resultObj.getState()==-1){
            msg="获取列表失败";
            map.addAttribute("error",msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }else {
            map.addAttribute("shipList",resultObj.getObj());
            jsonStr = StateMsg.toJson(1, map);
            return jsonStr;
        }
    }



    /**
     * 获取指定船舶信息
     *
     * @param map
     * @param shipid
     * @param mode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "shipinfo.do", method = RequestMethod.POST)
    public String getAppointMmsi(ModelMap map, @RequestParam(value = "shipid", required = false) Integer shipid,
                                 @RequestParam(value = "mode", required = false) Integer mode) {
        String msg;
        String jsonStr="";
        if(mode==0){
            AppointTargetFuseRst appointTargetFuseRst = realTimeMMSIService.getAppointTargetSource(shipid);
            if (appointTargetFuseRst == null) {
                msg = "获取指定船舶信息失败";
                map.addAttribute("error", msg);
                jsonStr = StateMsg.toJson(-1, map);
            }else{
                jsonStr = StateMsg.toJsonObj(1, appointTargetFuseRst);
            }
        }else {
            AppointTarget appointTarget = realTimeMMSIService.getAppointTarget(shipid, mode);
            if (appointTarget == null) {
                msg = "获取指定船舶信息失败";
                map.addAttribute("error", msg);
                jsonStr = StateMsg.toJson(-1, map);
            }else{
                jsonStr = StateMsg.toJsonObj(1, appointTarget);
            }
        }
        return jsonStr;
    }

    /**
     * 获取指定船舶融合信息
     * @param map
     * @param shipid
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "shipinfosource.do", method = RequestMethod.POST)
    public String getAppointTargetSource(ModelMap map, @RequestParam(value = "shipid", required = false) int shipid) {
        String msg;
        String jsonStr;

        AppointTargetFuseRst appointTargetFuseRst = realTimeMMSIService.getAppointTargetSource(shipid);
        if (appointTargetFuseRst == null) {
            msg = "获取指定船舶信息失败";
            map.addAttribute("error", msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }

//        map.addAttribute("content",appointTarget);
        jsonStr = StateMsg.toJsonObj(1, appointTargetFuseRst);
        return jsonStr;
    }

    /**
     * 获取目标档案信息
     * @param map
     * @param shipid
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "shipbaseinfo.do", method = RequestMethod.POST)
    public String getShipBaseInfoFuse(ModelMap map
            , @RequestParam(value = "shipid", required = false) String shipid
            , @RequestParam(value = "shiptype", required = false) String shipytpe) {
        String msg;
        String jsonStr="";

        ResultObj resultObj=realTimeMMSIService.getListShipBaseInfo(shipid,shipytpe);
        if(resultObj.getState()==-1){
            msg = "获取船舶信息失败";
            map.addAttribute("result", msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }else if(resultObj.getState()==0){
            msg = "获取到的信息为空";
            map.addAttribute("result", msg);
            jsonStr = StateMsg.toJson(0, map);
            return jsonStr;
        }else{
            map.addAttribute("list",resultObj.getObj());
            jsonStr = StateMsg.toJson(1, map);
            return jsonStr;
        }
    }

    /**
     * 实时船舶列表展示（2D展示）-REST接口
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "shiplist.do", method = RequestMethod.GET)
    public String imTargetGet(ModelMap map) {
        String msg;
        String jsonStr;

        List<AppointTarget> list = realTimeMMSIService.realTimeTargetGet();
        if (list.size()==0) {
            msg = "实时目标获取失败";
            map.addAttribute("result", msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }

        map.addAttribute("shiplist", list);
        jsonStr = StateMsg.toJson(1, map);
        return jsonStr;
    }

    /**
     * 实时区域统计
     *
     * @param map
     * @param ldlon
     * @param ldlat
     * @param rulon
     * @param rulat
     * @param mode  0：融合模式，1：原始模式
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "areacount.do", method = RequestMethod.POST)
    public String getSquAreaCount(ModelMap map, @RequestParam(value = "ldlon", required = false) double ldlon,
                                  @RequestParam(value = "ldlat", required = false) double ldlat,
                                  @RequestParam(value = "rulon", required = false) double rulon,
                                  @RequestParam(value = "rulat", required = false) double rulat,
                                  @RequestParam(value = "mode", required = false) int mode) {
        String msg;
        String jsonStr;

        List<CountryTPCount> countryTPCountList = realTimeMMSIService.getSquAreaCount(ldlon, ldlat, rulon, rulat, mode);

        if (countryTPCountList.size()==0) {
            msg = "获取失败";
            map.addAttribute("result", msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }

        int Co = realTimeMMSIService.areaCount(ldlon, ldlat, rulon, rulat, mode);
        if (Co == -1) {
            msg = "获取失败";
            map.addAttribute("result", msg);
            jsonStr = StateMsg.toJson(-2, map);
            return jsonStr;
        } else if (Co == -2) {
            msg = "获取失败";
            map.addAttribute("result", msg);
            jsonStr = StateMsg.toJson(-3, map);
            return jsonStr;
        }

        map.addAttribute("areacount", Co);
        map.addAttribute("areacountlist", countryTPCountList);
        jsonStr = StateMsg.toJson(1, map);
        return jsonStr;
    }

    /**
     * 实时船舶栅格数量统计
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "gridCountX1.do",method = RequestMethod.POST)
    public String gridCountX1(ModelMap map,@RequestParam(value="timeout", required = false) Integer timeout
    ,@RequestParam(value="mode", required = false) Integer mode){
        String jsonStr="";
        ResultObj resultObj=realTimeMMSIService.getGridShips(timeout,mode);
        if (resultObj.getState()==-1){
            jsonStr = StateMsg.toJsonOSE(StateEnum.StateCode.SQLExcuteError, map);
            return jsonStr;
        }else if(resultObj.getState()==0){
            jsonStr = StateMsg.toJsonOSE(StateEnum.StateCode.SelectNull, map);
            return jsonStr;
        }else{
            map.addAttribute("data",resultObj.getObj());
            jsonStr = StateMsg.toJsonOSE(StateEnum.StateCode.Success_All, map);
            return jsonStr;
        }
    }

    /**
     * 获取栅格船舶速度和
     * @param map
     * @param timeout
     * @param mode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getGridShipsSumSpeed.do",method = RequestMethod.POST)
    public String getGridShipsSumSpeed(ModelMap map,@RequestParam(value="timeout", required = false) Integer timeout
            ,@RequestParam(value="mode", required = false) Integer mode){
        String jsonStr="";
        ResultObj resultObj=realTimeMMSIService.getGridShipsSumSpeed(timeout,mode);
        if (resultObj.getState()==-1){
            jsonStr = StateMsg.toJsonOSE(StateEnum.StateCode.SQLExcuteError, map);
            return jsonStr;
        }else if(resultObj.getState()==0){
            jsonStr = StateMsg.toJsonOSE(StateEnum.StateCode.SelectNull, map);
            return jsonStr;
        }else{
            map.addAttribute("data",resultObj.getObj());
            jsonStr = StateMsg.toJsonOSE(StateEnum.StateCode.Success_All, map);
            return jsonStr;
        }
    }

    /**
     *
     * @param map
     * @param timeout
     * @param mode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getGridShipsAvgSpeed.do",method = RequestMethod.POST)
    public String getGridShipsAvgSpeed(ModelMap map,@RequestParam(value="timeout", required = false) Integer timeout
            ,@RequestParam(value="mode", required = false) Integer mode){
        String jsonStr="";
        ResultObj resultObj=realTimeMMSIService.getGridShipsAvgSpeed(timeout,mode);
        if (resultObj.getState()==-1){
            jsonStr = StateMsg.toJsonOSE(StateEnum.StateCode.SQLExcuteError, map);
            return jsonStr;
        }else if(resultObj.getState()==0){
            jsonStr = StateMsg.toJsonOSE(StateEnum.StateCode.SelectNull, map);
            return jsonStr;
        }else{
            map.addAttribute("data",resultObj.getObj());
            jsonStr = StateMsg.toJsonOSE(StateEnum.StateCode.Success_All, map);
            return jsonStr;
        }
    }
}